// 时间
/**
 * @param {Array} data
 */
export function convertDate(data) {
  const date = new Date(data)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const formattedDate = `${year}-${month}-${day}`
  return formattedDate
}
/**
 * @param {Array} datatime
 */
export function convertDateTime(datatime) {
  const date = new Date(datatime)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  return formattedDate
}

// 去重
/**
 * @param {Array} combinedArray
 * @param {String} val
 */
export function singleDuplication(combinedArray, val) {
  const uniqueObjects = combinedArray.reduce((accumulator, obj) => {
    if (typeof (val) === 'object') {
      const fieldValues = val.map(field => obj[field]) // 获取字段对应的值
      const isDuplicate = accumulator.some(item =>
        val.every((field, index) => item[field] === fieldValues[index])
      )
      if (!isDuplicate) {
        accumulator.push(obj)
      }
    } else {
      const fieldValue = obj[val]
      if (!accumulator.some(item => item[val] === fieldValue)) {
        accumulator.push(obj)
      }
    }
    return accumulator
  }, [])
  return uniqueObjects
}

/**
 * @param {Array} data
 * @param {Array} result
 * @param {String} val
 */
export function multipleDuplication(data, result, val) {
  const combinedArray = [...data, ...result.data.pageItems]
  const uniqueObjects = combinedArray.reduce((accumulator, obj) => {
    if (typeof (val) === 'object') {
      const fieldValues = val.map(field => obj[field]) // 获取字段对应的值
      const isDuplicate = accumulator.some(item =>
        val.every((field, index) => item[field] === fieldValues[index])
      )
      if (!isDuplicate) {
        accumulator.push(obj)
      }
    } else {
      const fieldValue = obj[val]
      if (!accumulator.some(item => item[val] === fieldValue)) {
        accumulator.push(obj)
      }
    }
    return accumulator
  }, [])
  return uniqueObjects
}

// 防抖
/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

// 获取常量
export function getConstantByType(constants, type) {
  const tmp = constants.filter(constant => constant.type === type)
  if (tmp && tmp.length > 0) {
    return tmp[0].data
  }
  return []
}
